<?php

namespace App\Models\Game;


use App\Models\ItemIcon;
use Illuminate\Support\Facades\DB;

class item_template extends BaseGame
{

    protected $connection = 'mysql_world';
    protected $primaryKey = 'entry';
    protected $table = 'item_template';
    public $timestamps = false;
    protected $fillable = ['name','description','class','subclass','displayid','Quality','BuyCount','BuyPrice',
        'SellPrice','maxcount','InventoryType','stackable','startquest','RandomProperty','bonding',
        'ItemLevel','RequiredLevel','MaxDurability','dmg_type1','dmg_type2','dmg_min1','dmg_max1','dmg_min2','dmg_max2','delay','armor','block',
        'stat_type1','stat_value1', 'stat_type2','stat_value2', 'stat_type3','stat_value3', 'stat_type4','stat_value4',
        'stat_type5','stat_value5', 'stat_type6','stat_value6', 'stat_type7','stat_value7', 'stat_type8','stat_value8',
        'stat_type9','stat_value9', 'stat_type10','stat_value10', 'StatsCount',
        'holy_res','fire_res','nature_res','frost_res','shadow_res','arcane_res',
        'duration','flagsCustom'
        ];
//    protected $visible = ['entry', 'name','ItemLevel','RequiredLevel','iconPath'];

    protected $appends=['iconPath'];

    public function icon(){
        return $this->hasOne(ItemIcon::class,'display_id','displayid');
    }

    public static function search($key){
        $itemLocalList=self::leftJoin('item_template_locale as l','l.ID','=','item_template.entry')
            ->where(function ($query) {
                $query->where('l.locale', '=', config('conf.loca'))
                    ->orWhere('l.locale', null);
            })->where(function ($query)use($key) {
                $query->where('item_template.name','like',"%$key%")
                    ->orWhere('l.Name','like',"%$key%");
            })
            ->select(['item_template.*'])->get();
        return $itemLocalList;
    }

    public function getNameAttribute(){
        $item=DB::connection('mysql_world')->table('item_template_locale')->where(['ID'=>$this->entry,'locale'=>config('conf.loca')])->get()->all();
        if ($item) {
//            dd($item[0]->Name);
            return $item[0]->Name;
        }else{
            return $this->getOriginal('name');
        }
    }

    public function getDescriptionAttribute(){
        $item=DB::connection('mysql_world')->table('item_template_locale')->where(['ID'=>$this->entry,'locale'=>config('conf.loca')])->get()->all();
        if ($item) {
//            dd($item[0]->Name);
            return $item[0]->Description;
        }else{
            return $this->getOriginal('description');
        }
    }

    public function getIconPathAttribute(){
        return $this->icon?$this->icon->path():'';
    }
}
